package com.company.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import com.company.project.entity.BusiPatrolEntity;
import com.company.project.entity.SysDept;
import com.company.project.entity.SysUser;
import com.company.project.service.BigScreenService;
import com.company.project.service.BusiPatrolService;
import com.company.project.service.DeptService;
import com.company.project.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("bigScreenService")
public class BigScreenServiceImpl implements BigScreenService {
    @Resource
    private DeptService deptService;
    @Resource
    private UserService userService;
    @Resource
    private BusiPatrolService busiPatrolService;
    @Override
    public List<JSONObject> performance() throws BusinessException {

        List<JSONObject> result = new ArrayList<>();

        // 组织列表查询
        List<SysDept> deptList = deptService.getDeptAll();
        for (SysDept dept:deptList) {
            JSONObject deptObject = new JSONObject();
            deptObject.put("deptName", dept.getName());

            String deptId = dept.getId();
            // 组织架构下的用户ID查询
            LambdaQueryWrapper<SysUser> userWrapper = Wrappers.lambdaQuery();
            userWrapper.eq(SysUser::getDeptId, deptId);
            List<SysUser> users = userService.list(userWrapper);
            Integer countAll = 0;
            for (SysUser user:users) {
                String userId = user.getId();
                // 根据用户ID查询巡山次数
                LambdaQueryWrapper<BusiPatrolEntity> patrolWrapper = Wrappers.lambdaQuery();
                patrolWrapper.eq(BusiPatrolEntity::getUserId, userId);
                Integer numb = busiPatrolService.count(patrolWrapper);
                countAll += numb;
            }
            deptObject.put("numb", countAll);
            result.add(deptObject);
        }
        return result;
    }

    @Override
    public List<JSONObject> personDistr() throws BusinessException {
        List<JSONObject> result = new ArrayList<>();

        // 组织列表查询
        List<SysDept> deptList = deptService.getDeptAll();
        for (SysDept dept:deptList) {
            JSONObject d = new JSONObject();
            d.put("detpName", dept.getName());
            String deptId = dept.getId();
            // 查询该组下有多少人
            LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(SysUser::getDeptId, deptId);
            Integer numb = userService.count(wrapper);
            d.put("numb", numb);
            result.add(d);
        }
        return result;
    }

    @Override
    public List<JSONObject> salary() throws BusinessException {
        List<JSONObject> result = new ArrayList<>();
        // 查询部门
        List<SysDept> deptList = deptService.getDeptAll();
        for (SysDept dept:deptList) {
            JSONObject d = new JSONObject();
            d.put("dept", dept.getName());
            String deptId = dept.getId();
            // 统计每个部门下工资总和
            LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(SysUser::getDeptId, deptId);
            List<SysUser> users = userService.list(wrapper);
            Integer salaryAll = 0;
            for (SysUser user:users) {
                String wage = user.getWages();
                if (StringUtils.isNotBlank(wage)) {
                    salaryAll += Integer.parseInt(user.getWages());
                }
            }
            d.put("salary", salaryAll);
            result.add(d);
        }
        return result;
    }

    @Override
    public List<JSONObject> ageGroup() throws BusinessException {

        Integer age010 = 0;
        Integer age1020 = 0;
        Integer age2030 = 0;
        Integer age3040 = 0;
        Integer age4050 = 0;
        Integer age5060 = 0;
        Integer age60up = 0;
        // 查询所有用户
        List<SysUser> allUsers = userService.list();
        for (SysUser user:allUsers) {
            Integer age = user.getAge();
            if (age == null) continue;
            if (age<10) age010+=age;
            else if(age<20) age1020++;
            else if(age<30) age2030++;
            else if(age<40) age3040++;
            else if(age<50) age4050++;
            else if(age<60) age5060++;
            else age60up++;
        }

        List<JSONObject> ageGroup = new ArrayList<>();

        JSONObject age010Object = new JSONObject();
        age010Object.put("ageGroup", 0);
        age010Object.put("numb", age010);
        ageGroup.add(age010Object);

        JSONObject age1020Object = new JSONObject();
        age1020Object.put("ageGroup", 1);
        age1020Object.put("numb", age1020);
        ageGroup.add(age1020Object);

        JSONObject age2030Object = new JSONObject();
        age2030Object.put("ageGroup", 2);
        age2030Object.put("numb", age2030);
        ageGroup.add(age2030Object);

        JSONObject age3040Object = new JSONObject();
        age3040Object.put("ageGroup", 3);
        age3040Object.put("numb", age3040);
        ageGroup.add(age3040Object);

        JSONObject age4050Object = new JSONObject();
        age4050Object.put("ageGroup", 4);
        age4050Object.put("numb", age4050);
        ageGroup.add(age4050Object);

        JSONObject age5060Object = new JSONObject();
        age5060Object.put("ageGroup", 5);
        age5060Object.put("numb", age5060);
        ageGroup.add(age5060Object);

        JSONObject age60upObject = new JSONObject();
        age60upObject.put("ageGroup", 6);
        age60upObject.put("numb", age60up);
        ageGroup.add(age60upObject);

        return ageGroup;
    }

    @Override
    public List<JSONObject> sex() throws BusinessException {
        // 查询所有用户
        List<SysUser> allUsers = userService.list();

        Integer man = 0;
        Integer woman = 0;

        final Integer MAN_CODE = 1;
        final Integer WOMAN_CODE = 2;

        for (SysUser user:allUsers) {
            Integer sex = user.getSex();
            if (sex == null) continue;
            if (MAN_CODE == sex) man++;
            else if (WOMAN_CODE == sex) woman++;
        }

        List<JSONObject> group = new ArrayList<>();

        JSONObject manObject = new JSONObject();
        manObject.put("sex", MAN_CODE);
        manObject.put("numb", man);
        group.add(manObject);

        JSONObject womanObject = new JSONObject();
        womanObject.put("sex", WOMAN_CODE);
        womanObject.put("numb", woman);
        group.add(womanObject);

        return group;
    }

}
